package com.kc.business.service.impl;

import com.kc.business.domain.*;
import com.kc.business.mapper.*;
import com.kc.business.service.IBizTourguideEnterService;
import com.kc.common.constant.BizConstants;
import com.kc.common.constant.UserConstants;
import com.kc.common.exception.CustomException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * TODO
 *
 * @author zhangshuo
 * @version 1.0
 * @date 2020/9/25
 */
@Service
public class BizTourguideEnterServiceImpl implements IBizTourguideEnterService {

    @Resource
    BizTourguideEnterMapper tourguideEnterMapper;
    @Resource
    BizTourguideMapper tourguideMapper;
    @Resource
    BizRoleMapper bizRoleMapper;
    @Resource
    BizUserMapper userMapper;
    @Resource
    BizUserRoleMapper bizUserRoleMapper;
    @Resource
    BizUserMapper bizUserMapper;

    /**
     * 导游入驻列表
     *
     * @param tourguideEnterForm
     * @return
     */
    @Override
    public List<BizTourguideEnterForm> getList(BizTourguideEnterForm tourguideEnterForm) {
        return tourguideEnterMapper.select(tourguideEnterForm);
    }

    /**
     * 修改导游入驻申请
     *
     * @param tourguideEnterForm
     * @return
     */
    @Override
    @Transactional
    public int edit(BizTourguideEnterForm tourguideEnterForm) {
        if (BizConstants.PASS_VERIFY.equals(tourguideEnterForm.getStatus())) {
            BizTourguideEnterForm enterForm = tourguideEnterMapper.selectByPrimaryKey(tourguideEnterForm.getEnterId());
            int useId = enterForm.getUserId();
            //更新APP用户
            BizUser bizUser = new BizUser();
            bizUser.setId(useId);
            bizUser.setIsTourguide(BizConstants.YES);
            userMapper.updateByPrimaryKeySelective(bizUser);

            //创建导游
            BizTourguide bizTourguide = new BizTourguide();
            bizTourguide.setUserId(useId);
            bizTourguide.setUserPath(enterForm.getAvatar());
            bizTourguide.setName(enterForm.getName());
            bizTourguide.setIdcardImg(enterForm.getIdCardImg());
            bizTourguide.setPhone(enterForm.getPhone());
            bizTourguide.setLatitude(enterForm.getLatitude());
            bizTourguide.setLongitude(enterForm.getLongitude());
            bizTourguide.setAdcode(enterForm.getAdcode());
            tourguideMapper.insertSelective(bizTourguide);

            //查询APP角色
            BizRole appRole = bizRoleMapper.getActiveByRoleKey(UserConstants.ROLE_GUIDE_APP);
            if (appRole == null) {
                throw new CustomException("请联系管理员，创建APP角色");
            }

            //设置APP用户角色
            BizUserRole bizUserRole = new BizUserRole();
            bizUserRole.setUserId(bizUser.getId());
            bizUserRole.setRoleId(appRole.getId());
            bizUserRoleMapper.insert(bizUserRole);
        }
        return tourguideEnterMapper.updateByPrimaryKeySelective(tourguideEnterForm);
    }
}
